/**
 * @class   DeviceX10
 * @brief   
 *
 *
 *
 ****************************************************************************
 * @version $Id: DeviceX10.h 2863 2010-08-07 04:28:58Z alex $
 * @author  Alessandro Polo
 ****************************************************************************/
/* Copyright (c) 2007-2010, WOSH - Wide Open Smart Home 
 * by Alessandro Polo - OpenSmartHome.com
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of the OpenSmartHome.com WOSH nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY Alessandro Polo ''AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL Alessandro Polo BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 ****************************************************************************/

#ifndef __WOSH_DeviceX10_H__
 #define __WOSH_DeviceX10_H__

 #include <woshDefs.h>
 #include <core/Variant.h>
 #include <core/List.h>
 #include <core/Object.h>
 #include <core/Serialization.h>
 #include <core/InterfacesProvider.h>
 #include <core/DataModels.h>
 #include <core/Device.h>
 #include <interfaces/Device.h>
 #include <interfaces/devices/DeviceX10.h>
 #include <interfaces/devices/Switch.h>
 #include <interfaces/devices/Dimmer.h>
 #include <interfaces/devices/SensorLight.h>
 #include <interfaces/devices/SensorMotion.h>
 
 #include <vector>
 

int x10codeStr2int( char* code );

struct codeX10 {
	unsigned short housecode; ///< A => 0
	unsigned short unitcode;  ///< 1 => 1
};

codeX10 string2codeX10( const std::string& code );
int string2codeX10num( const std::string& code );

std::string codeX102string( const codeX10& code );


class DeviceX10 : public wosh::Object
 {
	WOSH_CLASSNAME(DeviceX10)
//	WOSH_SERIALIZABLE(DeviceX10)
//	WOSH_COMPARE_RTTI(DeviceX10)

	public:

		enum MODULE_TYPE {
			UNKNOWN,

			AM12,

			LM12,
			LM12W,
			LM12I,
			
			LM15,
			
			ML15ES,
			
			LD11,
			
			AWM2,
			
			MS13,
			MS13E,
			
			AD10,
			AW12,
			
			SM10,
			SM10E
		 };

		enum MODULE_ACTION {
			EVENT_UNKNOWN = 0,
			EVENT_ON = 1,
			EVENT_OFF = 2,
			EVENT_DIM = 4,
			EVENT_BRIGHT = 8
			
		 };


	public:
		DeviceX10( const codeX10& code );
		DeviceX10( const DeviceX10& m );
		virtual ~DeviceX10();

	public:
		DeviceX10& operator=(const DeviceX10& m);
		bool operator==(const DeviceX10 &other) const;

/** @name Getters
 * @{
 ******************************************************************************/
	public:
		inline const std::string& getX10Code() const		{ return this->x10codeStr; }
		inline char getHouseCode() const					{ return this->x10code.housecode; }
		inline unsigned short getUnitCode() const			{ return this->x10code.unitcode; }

		inline const std::string& getAlias() const			{ return this->alias; }

		inline int getX10State() const						{ return this->state; }

		inline DeviceX10::MODULE_TYPE getModuleType() const			{ return this->type; }

		inline void setModuleType( DeviceX10::MODULE_TYPE mtype )	{ this->type = mtype; }

		inline const char* getX10ModuleNameAsString() const			{ return getX10ModuleNameAsString(this->type); }

//		std::string getTypeAsString() const;
//		std::string getX10ModuleNameAsString() const;

		// mapping with real fields
		virtual wosh::Variant getProperty( const std::string& key ) const;

		virtual std::vector<std::string> getKeys() const;

		virtual bool isSwitch() const;
		virtual bool isDimmer() const;
		virtual bool isSensor() const;

		virtual inline const wosh::InterfacesProvider& getInterfaces() const	{ return this->Interfaces; }
		virtual inline wosh::InterfacesProvider& getInterfaces()				{ return this->Interfaces; }

		virtual void getMethods( wosh::List& methods ) const;

		static const char* getX10ModuleNameAsString( DeviceX10::MODULE_TYPE mtype );
		static DeviceX10::MODULE_TYPE getX10ModuleNameFromString( const std::string& ntype );

		static wosh::DataFieldCollector<DeviceX10>* getDataFields();

//@}
/** @name Setters
 * @{
 ******************************************************************************/
	public:
		void setX10Code( const codeX10& x10code );
		void setHouseCode( char housecode );
		void setUnitCode( unsigned short unitcode );
		void setX10Code( const std::string& x10code );

		inline void setX10Alias( const std::string& alias ) { this->alias = alias; setName(alias); }

		void setX10State( int state ) { this->state = state; }


		// mapping with real fields
		virtual WRESULT setProperty(const std::string& key, const wosh::Variant& value);

//@}
/** @name Serialization interface
 * @{
 ******************************************************************************/
	public:
		virtual std::string toString() const;
		virtual bool updateHash( wosh::DataHashContext* hash_context ) const;
//@}

	protected:
		virtual void clear();

	protected:
		codeX10 x10code;			///< pure X10 code (struct)
		std::string x10codeStr;		///< shadow x10 code
		MODULE_TYPE type;			///< module type
		std::string alias;			///< alias name (human friendly code/name)
		int state;					///< state of the device
		wosh::InterfacesProvider Interfaces; 	///< supported interface


}; // class def


#endif //__WOSH_DeviceX10_H__
